package wsem.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class IndexJDBC {
	
	private static String URL = "jdbc:mysql://localhost/ws_db";
	private static String LOGIN = "root";
	private static String PASSWORD = "";
	
	private Connection connect = null;
	
	
	
	public void Connection(String url, String login, String password) {

		try {
			connect = (com.mysql.jdbc.Connection) DriverManager.getConnection(
					url, login, password);
			connect.setAutoCommit(false);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * disconnect from the data base
	 */
	public void Disconnect() {
		try {
			if (connect != null) {
				connect.close();
			}
		} catch (Exception e) {

		}
	}
	
	public IndexJDBC() {
		
		
		// This will load the MySQL driver, each DB has its own driver
				try {
					Class.forName("com.mysql.jdbc.Driver");
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

	}


	/**
	 * add a list of terme to the data base
	 * @param collection the collection of terms to add
	 * @throws SQLException exception thrown when could not insert in the data base 
	 */
	public void ajouterTerme(List<Termes> collection) throws SQLException {

		String queryString = "INSERT INTO `ws_db`.`term` (`idTerm`, `term`) VALUES (?, ?);";

		PreparedStatement prepare_terme = null;
		prepare_terme = (PreparedStatement) connect
				.prepareStatement(queryString);
		int compteur = 0;
		try {
			for (Termes term : collection) {
				compteur++;
				prepare_terme.setInt(1, term.getIdTerm());
				prepare_terme.setString(2, term.getTerme());
				prepare_terme.addBatch();
				if (compteur % 1000 == 0) {
					prepare_terme.executeBatch();
					prepare_terme.clearBatch();
				}
			}
			prepare_terme.executeBatch();
			prepare_terme.clearBatch();
			connect.commit();

		} catch (Exception e) {
			System.out
					.println("Une erreur lors de l'ajout d'un terme dans la bd a eu lieu");
			e.printStackTrace();
			// TODO: handle exception
		} finally {
			prepare_terme.close();
		}

	}

	/**
	 * add a list of document to the data base
	 * @param collection the collection of documents to add
	 * @throws SQLException exception thrown when could not insert in the data base 
	 */
	public void ajouterDocument(List<Document> collection) throws SQLException {
		String queryString = "INSERT INTO `ws_db`.`doc` (`idDoc`, `nomDoc`) VALUES (?,?);";

		PreparedStatement prepare_terme = null;
		prepare_terme = (PreparedStatement) connect
				.prepareStatement(queryString);
		int compteur = 0;
		try {
			for (Document document : collection) {
				compteur++;
				prepare_terme.setInt(1, document.getIdDoc());
				System.err.println("id doc "+document.getIdDoc());
				prepare_terme.setString(2, document.getNomDoc());
				prepare_terme.addBatch();
				if (compteur % 1000 == 0) {
					prepare_terme.executeBatch();
					prepare_terme.clearBatch();
				}
			}
			prepare_terme.executeBatch();
			prepare_terme.clearBatch();
			connect.commit();

		} catch (Exception e) {
			System.out
					.println("Une erreur lors de l'ajour d'un terme dans la bd a eu lieu");
			e.printStackTrace();
			// TODO: handle exception
		} finally {
			prepare_terme.close();
		}

	}

	/**
	 * add a list of nodes to the data base
	 * @param collection the collection of nodes to add
	 * @throws SQLException exception thrown when could not insert in the data base 
	 */
	public void ajouterNoeud(List<Noeud> collection) throws SQLException {
		String queryString = "INSERT INTO `ws_db`.`noeud` (`idNoeud`, `xpath`, `idDoc`) VALUES (?,?,?);";

		PreparedStatement prepare_terme = null;
		prepare_terme = (PreparedStatement) connect
				.prepareStatement(queryString);
		int compteur = 0;
		try {
			for (Noeud noeud : collection) {
				compteur++;
				prepare_terme.setInt(1, noeud.getIdNoeud());
				prepare_terme.setString(2, noeud.getXpath());
				prepare_terme.setInt(3, noeud.getIdDoc());
				prepare_terme.addBatch();
				if (compteur % 1000 == 0) {
					prepare_terme.executeBatch();
					prepare_terme.clearBatch();
				}
			}
			prepare_terme.executeBatch();
			prepare_terme.clearBatch();
			connect.commit();

		} catch (Exception e) {
			System.out
					.println("Une erreur lors de l'ajout d'un noeud dans la bd a eu lieu");
			e.printStackTrace();
			// TODO: handle exception
		} finally {
			prepare_terme.close();
		}

	}

	/**
	 * add a list of Information to the data base. an information is a link between a terme and nodes he belongs to
	 * @param collection the collection of Information to add
	 * @throws SQLException exception thrown when could not insert in the data base 
	 */
	public void ajouterTermNode(List<Term_noeud> collection)
			throws SQLException {
		String queryString = "INSERT INTO `ws_db`.`term_noeud` (`idTerm`, `idNoeud`, `poids`, `frequence`, `listPos`) VALUES (?,?,?,?,?);";

		PreparedStatement prepare_terme = null;
		prepare_terme = (PreparedStatement) connect
				.prepareStatement(queryString);
		int compteur = 0;
		try {
			for (Term_noeud info : collection) {
				compteur++;
				prepare_terme.setInt(1,info.getIdTerm());
				prepare_terme.setInt(2, info.getIdNoeud());
				prepare_terme.setDouble(3, info.getPoids());
				
				prepare_terme.setDouble(4, info.getFrequence());
				prepare_terme.setString(5, "(1,6,15,56)");
				prepare_terme.addBatch();
				if (compteur % 1000 == 0) {
					prepare_terme.executeBatch();
					prepare_terme.clearBatch();
				}
			}
			prepare_terme.executeBatch();
			prepare_terme.clearBatch();
			connect.commit();

		} catch (Exception e) {
			System.out
					.println("Une erreur lors de l'ajour d'un terme node dans la bd a eu lieu");
			e.printStackTrace();
			// TODO: handle exception
		} finally {
			prepare_terme.close();
		}

	}

	public void deletebdd() {

		Statement statement = null;
		try {
			statement = (Statement) connect.createStatement();
			statement.addBatch("DELETE FROM `term_noeud`;");
			statement.addBatch("DELETE FROM `noeud`;");
			statement.addBatch("DELETE FROM `term`;");
			
			
			statement.addBatch("DELETE FROM `doc`;");
			

			
			// execute all in once
			statement.executeBatch();
			statement.clearBatch();
			//Commit
			connect.commit();

		} catch (SQLException e) {
			System.out.println("Erreur lors de la remise a zero de la BD");
		} finally {
			try {
				if (statement != null) {
					statement.close();
				}
			} catch (SQLException e) {
				System.out.println("Erreur lors de la remise a zero de la BD");
			}

		}
	}





	
	


}
